﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ePlanet.Entities;
using ePlanet.ArchiveMange.DataAccess;
using System.Data;

namespace Web.Views.DataManage.PLuploads
{
    /// <summary>
    /// ArchiveNumRepeatQueryHandler 的摘要说明
    /// </summary>
    public class ArchiveNumRepeatQueryHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            try 
            {
                string catalogId = context.Request.QueryString["CatalogId"];
                string requestData = context.Request.Form["data"].ToString();
                List<ArchiveNumQueryEntity> entities = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ArchiveNumQueryEntity>>(requestData);
                ArchiveCommon archiveCommonDal = new ArchiveCommon();
                IList<string> archiveNums = new List<string>();
                foreach (ArchiveNumQueryEntity item in entities)
                {
                    archiveNums.Add(item.ArchiveNum);
                }
                DataTable table = archiveCommonDal.GetArchiveByArchiveNums(archiveNums, catalogId);
                List<ArchiveNumQueryEntity> list = new List<ArchiveNumQueryEntity>();
                if (table.Rows.Count > 0)
                {
                    foreach (ArchiveNumQueryEntity item in entities)
                    {
                        foreach (DataRow row in table.Rows)
                        {
                            if (item.ArchiveNum == string.Concat(row["ArchiveNum"]))
                            {
                                ArchiveNumQueryEntity entity = new ArchiveNumQueryEntity();
                                entity.DataBaseEntityId = string.Concat(row["DataId"]);
                                entity.EntityId = item.EntityId;
                                entity.ArchiveNum = item.ArchiveNum;
                                entity.Title = string.Concat(row["Title"]);
                                list.Add(entity);
                                break;
                            }
                        }
                    }
                }
                string responseStr = Newtonsoft.Json.JsonConvert.SerializeObject(list);
                context.Response.Write(responseStr);
            }
            catch (Exception ex)
            {
                context.Response.Write("Error: "+ex.Message);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}